<?php
/**
* @version 1.0.0
* @package RSSearch! 1.0.0
* @copyright (C) 2011 www.rsjoomla.com
* @license GPL, http://www.gnu.org/copyleft/gpl.html
*/
defined('_JEXEC') or die('Restricted access');

jimport('joomla.form.formfield');
class JFormFieldK2categoriesfields extends JFormField
{
	/**
	 * The form field type.
	 *
	 * @var		string
	 * @since	1.6
	 */
	protected $type = 'K2categoriesfields';

	/**
	 * Method to get the field input markup.
	 *
	 * @return	string	The field input markup.
	 * @since	1.6
	 */
	protected function getInput()
	{
		if(file_exists(JPATH_SITE.DS.'components'.DS.'com_k2'.DS.'k2.php')){
			$result = '<script type="text/javascript">
					window.addEvent(\'domready\',function(){
						if(document.getElementById("k2_categories_btn").checked){
							document.getElementById("listk2_categories").style.visibility="visible";
							document.getElementById("desc_k2_categories").style.visibility="hidden";
						}else {
							document.getElementById("listk2_categories").style.visibility="hidden";
							document.getElementById("desc_k2_categories").style.visibility="visible";
						}
					});
					</script>';
			$result .= '<div id="desc_k2_categories" style="visibility:visible;float:right;margin-top: 8px;">'.JText::_('RSF_K2_CATEGORIES_LIST_DESC').'</div>';
			$result .= '<div id="listk2_categories" style="visibility:hidden;">';
	
				$db =& JFactory::getDBO();
				$query = "SELECT * FROM #__k2_categories ORDER BY `name` ASC";
				$db->setQuery($query);
				$cats = $db->loadObjectList();
				$value = (isset($this->value['k2categories']) ? $this->value['k2categories'] : array());
				
				
			$result .= JHTML::_('select.genericlist', $cats, $this->name.'[k2categories][]', 'multiple="multiple"', 'id', 'name', $value);
			$result .= '</div>';
		} else $result = 'You need to install K2 Component in order to use RSSearch! for K2 plugin. You can download K2 component from <a href="http://getk2.org/" target="_blanc">here</a>';
		
		return $result;
	}
}